package com.xialinrui.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xialinrui.entity.*;
import com.xialinrui.service.NoteCategoryService;
import com.xialinrui.service.NoteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xialinrui
 */
@RestController
@Slf4j
@RequestMapping("/note")
public class NoteController {

    @Resource
    private NoteService noteService;

    @Resource
    private NoteCategoryService noteCategoryService;

    @PostMapping("/add")
    public ResponseResult<Map<String, Integer>> addMood(@RequestBody Note note) {
        noteService.addOneInstant(note);
        HashMap<String, Long> resMap = new HashMap<>();
        resMap.put("id", note.getId());
        return new ResponseResult(200, "创建成功", resMap);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody Note note) {
        noteService.updateById(note);
        return new ResponseResult(200, "保存成功");
    }

    @DeleteMapping("/delete")
    public ResponseResult delete(@RequestBody List<Long> ids) {
        noteService.delete(ids);
        return new ResponseResult(200, "删除成功");
    }


    /**
     * 查询所有信息
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<Page<Note>> getAll(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam Long noteCategoryId) {
        Page<Note> resPage = new Page<>(page, pageSize);
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("note_category_id", noteCategoryId).orderByDesc("update_time").orderByDesc("create_time");
        noteService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }


    /**
     * 根据输入框模糊查询信息库
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("list/like")
    public ResponseResult<Page<Note>> getLike(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam String content) {
        Page<Note> resPage = new Page<>(page, pageSize);
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", content).orderByDesc("update_time").orderByDesc("create_time");
        noteService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }


    /**
     * 根据id显示文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/show")
    public ResponseResult<MoodRecord> show(@RequestParam Long id) {
        return new ResponseResult(200, "显示成功", noteService.getById(id));
    }


    /**
     * 根据时间筛选文章
     *
     * @param from
     * @param to
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/list/date")
    public ResponseResult<Page<Note>> getByDate(@RequestParam Date from, @RequestParam Date to, @RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<Note> resPage = new Page<>(page, pageSize);
        QueryWrapper<Note> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("create_time", from, to).orderByDesc("update_time").orderByDesc("create_time");
        noteService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }


    @PostMapping("/add/category")
    public ResponseResult<Map<String, Long>> addCategory(@RequestParam String name) {
        Map<String, Long> resMap = new HashMap<>();
        QueryWrapper<NoteCategory> noteCategoryQueryWrapper = new QueryWrapper<>();
        noteCategoryQueryWrapper.eq("name", name);
        NoteCategory category = noteCategoryService.getOne(noteCategoryQueryWrapper);
        if (category != null) {
            return new ResponseResult(400, "已经添加过该分类了哦～");
        }
        noteCategoryService.addCategory(name);
        category = noteCategoryService.getOne(noteCategoryQueryWrapper);
        resMap.put("id", category.getId());
        return new ResponseResult(200, "分类添加成功～", resMap);
    }

    @PutMapping("/change/category")
    public ResponseResult changeCategory(@RequestParam Long categoryId, @RequestParam Long noteId) {
        UpdateWrapper<Note> noteUpdateWrapper = new UpdateWrapper<>();
        noteUpdateWrapper.set("note_category_id", categoryId).eq("id", noteId);
        noteService.update(noteUpdateWrapper);
        return new ResponseResult(200, "修改成功");
    }


    @PutMapping("/category/rename")
    public ResponseResult renameCategory(@RequestParam Long categoryId, @RequestParam String newName) {
        UpdateWrapper<NoteCategory> noteCategoryUpdateWrapper = new UpdateWrapper<>();
        noteCategoryUpdateWrapper.set("name", newName).eq("id", categoryId);
        noteCategoryService.update(noteCategoryUpdateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    /**
     * 查询所有分类
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/category/list")
    public ResponseResult<Page<NoteCategory>> getCategoryList(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<NoteCategory> resPage = new Page<>(page, pageSize);
        QueryWrapper<NoteCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("name");
        noteCategoryService.page(resPage, queryWrapper);
        return new ResponseResult(200, "查询成功", resPage);
    }


    @DeleteMapping("/delete/category")
    public ResponseResult deleteCategory(@RequestBody List<Long> ids) {
        noteCategoryService.delete(ids);
        return new ResponseResult(200, "删除成功");
    }
}
